package com.iris.live.services.data.repositories;

import com.iris.live.services.data.models.销售财务表Model;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

/**
 * Created by cyl on 2017/12/12.
 */
public interface 销售财务表Repository extends JpaRepository<销售财务表Model,Integer>,JpaSpecificationExecutor<销售财务表Model>{

    @Query("select a from 销售财务表Model a where a.订单编号 = ?1")
    销售财务表Model findByOrderNumber(String orderNumber);

    @Modifying
    @Query("update 销售财务表Model set 订单总价 = ?2 where 订单编号 = ?1")
    int updateOrderTotalAmount(String intiQuotedNum,Double amount);

    @Modifying
    @Query("update 销售财务表Model set 收款状态 = ?2 where 订单编号 = ?1")
    int updateReceivablesStatus(String intiQuotedNum,String status);

    @Modifying
    @Query("update 销售财务表Model set 开票状态 = ?2 where 订单编号 = ?1")
    int updateInvoiceStatus(String initQuotedNum,String status);

    /**
     * 获取订单的实收金额
     * @param initNum
     * @return
     */
    @Query(value = "select ifnull(a.收款金额,0) + ifnull(a.红冲收款金额,0) - (ifnull(a.退款金额,0) + ifnull(a.红冲退款金额,0)) from 销售财务表 a where a.订单编号 = ?1",nativeQuery = true)
    String getActualReceiveMoney(String initNum);

}
